﻿using System.Web;
using SY.WebSite.BLL;
using SY.WebSite.Utility;
using SY.Uap.Entity;

namespace SY.WebSite.Web
{
    /// <summary>
    /// Web Handler基类
    /// </summary>
    public class ApiWebHandler : BaseWebHandler
    {
        public User CurrentUser { get; set; }
        public string OS { get; set; }
        public string Version { get; set; }

        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            string appId = Request.Headers["appId"] ?? RequestStringParam("appId");
            string appKey = Request.Headers["appKey"] ?? RequestStringParam("appKey");
            OS = Request.Headers["os"] ?? RequestStringParam("os");
            Version = Request.Headers["version"] ?? RequestStringParam("version");

            if (appId != "38D03C41CEB24626A92EC7D8AC54F4FB" || appKey != "DBAD065C23EE4a0bBB50B665E56836BD")
            {
                BreakResponse("非法请求");
                return;
            }

            int userId = RequestIntParam("userId");
            if (userId > 0)
            {
                CurrentUser = BizUser.GetByKey(userId);
                if (CurrentUser == null || CurrentUser.Status == 1)
                {
                    BreakResponse("账号不存在或被禁用");
                }
            }
        }

        public static int RequestIntParam(string key, int defaultValue = 0)
        {
            return Tools.Web.Request.GetFormInt(key, defaultValue);
        }

        public static string RequestStringParam(string key)
        {
            return Tools.Web.Request.GetFormString(key);
        }

        public override void BreakResponse(string message = "参数异常，请重试")
        {
            JsonTransfer jt = new JsonTransfer();
            jt.SetApiFailed(message);
            Response.Write(jt);
            Response.End();
        }
    }
}